 <template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @open="loadRoleList" @close="handleClose">
    <el-checkbox-group v-model="roleIds" v-loading="$store.state.logoing">
      <el-checkbox v-for="item in reqRoleIds" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="handleSubmit">确定</el-button>
      <el-button size="small" @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { reqGetRoleListAPI } from '@/api/setting.js'
import { getDetailInfo } from '@/api/user.js'
import { reqAssignRolesAPI } from '@/api/employees.js'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    userId: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      roleIds: [],
      reqRoleIds: []
    }
  },
  methods: {
    // 提交角色
    async handleSubmit () {
      try {
        const ret = await reqAssignRolesAPI({
          id: this.userId,
          roleIds: this.roleIds
        })
        if (ret.code === 10000) {
          // 关闭角色弹窗
          this.handleClose()
          // 给出提示
          this.$message.success('提交成功')
        }
      } catch {
        this.handleClose()
        this.$message.error('提交失败')
      }
    },
    // 关闭角色弹窗
    handleClose () {
      this.$emit('update:showRoleDialog', false)
    },
    async loadRoleList () {
      const ret = await reqGetRoleListAPI({
        page: 1,
        pagesize: 1000
      })
      const retq = await getDetailInfo(this.userId)
      Promise.all([ret, retq]).then((sub) => {
        this.reqRoleIds = sub[0].data.rows
        this.roleIds = sub[1].data.roleIds
      }).catch(() => {
        this.$message.error('获取角色失败')
      })
      // if (ret.code === 10000) {
      //   this.reqRoleIds = ret.data.rows
      // } else {
      //   this.$message.error('全部角色获取失败')
      // }

      // if (retq.code === 10000) {
      //   this.roleIds = retq.data.roleIds
      // } else {
      //   this.$message.error('个人用户获取失败')
      // }
    }
  }
}
</script>

<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
